// todo adapter
import loader from '../../../../../../../lib/plugins/loader.js'
import { hasGenshin, isTRSS } from '#guoba.adapter'

const CfgAdapter = await (() => {
  if (isTRSS) {
    return import('./useTRSSConfig.js')
  } else {
    return import('./useMiaoConfig.js')
  }
})()

const addGroupPromptProps = CfgAdapter['addGroupPromptProps']

// ========================================
// 基础配置
// ========================================
const baseConfig = {
  key: 'base',
  title: '基础配置',
  cards: [
    {
      key: 'system.bot',
      title: '机器人配置',
      desc: '对机器人进行相关配置',
      schemas: [
        {
          field: 'log_level',
          label: '日志等级',
          bottomHelpMessage: '日志输出等级。Mark时只显示执行命令，不显示聊天记录',
          component: 'Select',
          componentProps: {
            options: [
              { label: 'Trace', value: 'trace' },
              { label: 'Debug', value: 'debug' },
              { label: 'Info', value: 'info' },
              { label: 'Warn', value: 'warn' },
              { label: 'Fatal', value: 'fatal' },
              { label: 'Mark', value: 'mark' },
              { label: 'Error', value: 'error' },
              { label: 'Off', value: 'off' },
            ],
            placeholder: '请选择日志等级',
          },
        },
        {
          field: 'log_align',
          label: '日志头内容',
          bottomHelpMessage: '日志头内容自定义显示，例如设置为"XRKYZ"将显示[XRKYZ]',
          component: 'Input',
          componentProps: {
            placeholder: '请输入日志头内容',
          },
        },
        {
          field: 'log_color',
          label: '日志头颜色方案',
          bottomHelpMessage: '选择日志头的颜色主题',
          component: 'Select',
          componentProps: {
            options: [
              { label: '默认蓝系', value: 'default' },
              { label: '红橙黄绿蓝', value: 'scheme1' },
              { label: '粉色系', value: 'scheme2' },
              { label: '蓝绿色系', value: 'scheme3' },
              { label: '紫色系', value: 'scheme4' },
              { label: '天空渐变', value: 'scheme5' },
              { label: '火焰渐变', value: 'scheme6' },
              { label: '绿野渐变', value: 'scheme7' },
            ],
            placeholder: '请选择颜色方案',
          },
        },
        {
          field: 'log_id_length',
          label: '日志ID长度',
          bottomHelpMessage: '日志ID长度（默认16个字符）',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '请输入ID长度',
          },
        },
        {
          field: 'log_id_filler',
          label: 'ID美化字符',
          bottomHelpMessage: 'ID显示时的美化字符（用于填充空白）',
          component: 'Select',
          componentProps: {
            options: [
              { label: '.', value: '.' },
              { label: '·', value: '·' },
              { label: '─', value: '─' },
              { label: '•', value: '•' },
              { label: '═', value: '═' },
              { label: '»', value: '»' },
              { label: '→', value: '→' },
            ],
            placeholder: '请选择美化字符',
          },
        },
        {
          field: 'log_object',
          label: '日志对象检查',
          bottomHelpMessage: '日志对象检查配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'depth',
                label: '检查深度',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  placeholder: '默认10',
                },
              },
              {
                field: 'colors',
                label: '彩色输出',
                component: 'Switch',
              },
              {
                field: 'showHidden',
                label: '显示隐藏属性',
                component: 'Switch',
              },
              {
                field: 'showProxy',
                label: '显示代理对象',
                component: 'Switch',
              },
              {
                field: 'getters',
                label: '显示getters',
                component: 'Switch',
              },
              {
                field: 'breakLength',
                label: '换行长度',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  placeholder: '默认100',
                },
              },
              {
                field: 'maxArrayLength',
                label: '最大数组长度',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  placeholder: '默认100',
                },
              },
              {
                field: 'maxStringLength',
                label: '最大字符串长度',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  placeholder: '默认1000',
                },
              },
            ],
          },
        },
        {
          field: 'ignore_self',
          label: '过滤自己',
          bottomHelpMessage: '群聊和频道中过滤自己的消息',
          component: 'Switch',
        },
        {
          field: '/→#',
          label: '斜杠转井号',
          bottomHelpMessage: '自动把 / 换成 #',
          component: 'Switch',
        },
        {
          field: 'file_watch',
          label: '监听文件变化',
          bottomHelpMessage: '是否监听文件变化',
          component: 'Switch',
        },
        {
          field: 'online_msg_exp',
          label: '上线推送冷却',
          bottomHelpMessage: '上线推送通知的冷却时间（秒）',
          component: 'InputNumber',
          componentProps: {
            min: 0,
            placeholder: '默认86400秒',
          },
        },
        {
          field: 'file_to_url_time',
          label: '文件URL有效时间',
          bottomHelpMessage: '文件URL有效时间（分钟）',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '默认60分钟',
          },
        },
        {
          field: 'file_to_url_times',
          label: '文件URL访问次数',
          bottomHelpMessage: '文件URL访问次数限制',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '默认5次',
          },
        },
        {
          field: 'chromium_path',
          label: 'chromium路径',
          bottomHelpMessage: 'chromium其他路径，默认无需填写，需要时可填写chromium的可执行文件绝对路径',
          component: 'Input',
          componentProps: {
            placeholder: '请输入chromium路径',
          },
        },
        {
          field: 'puppeteer_ws',
          label: 'puppeteer接口地址',
          bottomHelpMessage: 'puppeteer接口地址，默认无需填写',
          component: 'Input',
          componentProps: {
            placeholder: '请输入puppeteer接口地址',
          },
        },
        {
          field: 'puppeteer_timeout',
          label: 'puppeteer截图超时时间',
          bottomHelpMessage: 'puppeteer截图超时时间，默认无需填写',
          component: 'InputNumber',
          componentProps: {
            min: 0,
            placeholder: '（毫秒）',
          },
        },
      ],
    },
    {
      key: 'system.db',
      title: '数据库配置',
      desc: '对Sequelize数据库进行相关配置',
      schemas: [
        {
          field: 'dialect',
          label: '数据库类型',
          required: true,
          component: 'Select',
          componentProps: {
            options: [
              { label: 'SQLite', value: 'sqlite' },
              { label: 'MySQL', value: 'mysql' },
              { label: 'MariaDB', value: 'mariadb' },
              { label: 'PostgreSQL', value: 'postgres' },
              { label: 'MSSQL', value: 'mssql' },
              { label: 'DB2', value: 'db2' },
            ],
            placeholder: '请选择数据库类型',
          },
        },
        {
          field: 'storage',
          label: 'SQLite文件地址',
          bottomHelpMessage: 'SQLite数据库文件路径',
          component: 'Input',
          componentProps: {
            placeholder: 'data/db/data.db',
          },
        },
        {
          field: 'logging',
          label: '日志输出',
          bottomHelpMessage: '是否输出SQL日志',
          component: 'Switch',
        },
      ],
    },
    {
      key: 'system.device',
      title: '设备管理配置',
      desc: '设备管理的核心参数配置',
      schemas: [
        {
          field: 'heartbeat_interval',
          label: '心跳发送间隔',
          bottomHelpMessage: '心跳发送间隔（秒），用于检测设备在线状态',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '默认30秒',
          },
        },
        {
          field: 'heartbeat_timeout',
          label: '心跳超时时间',
          bottomHelpMessage: '心跳超时时间（秒），超过此时间视为设备离线',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '默认120秒',
          },
        },
        {
          field: 'max_devices',
          label: '最大设备数量',
          bottomHelpMessage: '系统支持的最大设备数量',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '默认100',
          },
        },
        {
          field: 'max_logs_per_device',
          label: '设备最大日志条数',
          bottomHelpMessage: '每个设备存储的最大日志条数，超过将自动截断',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '默认100',
          },
        },
        {
          field: 'max_data_per_device',
          label: '设备最大数据条数',
          bottomHelpMessage: '每个设备存储的最大数据条数，超过将自动移除最早数据',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '默认50',
          },
        },
        {
          field: 'command_timeout',
          label: '命令执行超时',
          bottomHelpMessage: '命令执行超时时间（毫秒），超过视为超时',
          component: 'InputNumber',
          componentProps: {
            min: 100,
            placeholder: '默认5000毫秒',
          },
        },
        {
          field: 'batch_size',
          label: '批量发送数量',
          bottomHelpMessage: '命令批量发送的最大数量，用于优化性能',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '默认100',
          },
        },
      ],
    },
    {
      key: 'system.notice',
      title: '通知配置',
      desc: '配置各种通知服务',
      schemas: [
        {
          field: 'iyuu',
          label: 'IYUU Token',
          bottomHelpMessage: 'IYUU(https://iyuu.cn/)通知服务Token',
          component: 'Input',
          componentProps: {
            placeholder: '请输入IYUU Token',
          },
        },
        {
          field: 'sct',
          label: 'Server酱',
          bottomHelpMessage: 'Server酱(https://sct.ftqq.com/)通知服务SendKey',
          component: 'Input',
          componentProps: {
            placeholder: '请输入Server酱SendKey',
          },
        },
        {
          field: 'feishu_webhook',
          label: '飞书机器人Webhook',
          bottomHelpMessage: '飞书自定义机器人Webhook(https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot)',
          component: 'Input',
          componentProps: {
            placeholder: '请输入飞书机器人Webhook地址',
          },
        },
      ],
    },
    {
      key: 'system.renderer',
      title: '渲染器配置',
      desc: '配置渲染后端',
      schemas: [
        {
          field: 'name',
          label: '渲染后端',
          bottomHelpMessage: '选择渲染后端',
          component: 'Select',
          componentProps: {
            options: [
              { label: 'Puppeteer', value: 'puppeteer' },
            ],
            placeholder: '请选择渲染后端',
          },
        },
      ],
    },
    {
      key: 'system.aistream',
      title: '工作流系统配置',
      desc: '配置AI工作流系统',
      schemas: [
        {
          field: 'enabled',
          label: '启用工作流',
          bottomHelpMessage: '是否启用工作流系统',
          component: 'Switch',
        },
        {
          field: 'streamDir',
          label: '工作流目录',
          bottomHelpMessage: '工作流目录路径（相对于项目根目录）',
          component: 'Input',
          componentProps: {
            placeholder: 'plugins/stream',
          },
        },
        {
          field: 'global',
          label: '全局设置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'maxTimeout',
                label: '最大执行超时',
                bottomHelpMessage: '最大执行超时时间（毫秒）',
                component: 'InputNumber',
                componentProps: {
                  min: 1000,
                  placeholder: '默认30000毫秒',
                },
              },
              {
                field: 'debug',
                label: '调试日志',
                bottomHelpMessage: '是否启用调试日志',
                component: 'Switch',
              },
              {
                field: 'maxConcurrent',
                label: '并发执行限制',
                bottomHelpMessage: '最大并发执行数量',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  placeholder: '默认5',
                },
              },
            ],
          },
        },
        {
          field: 'cache',
          label: '缓存设置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'enabled',
                label: '启用缓存',
                bottomHelpMessage: '是否启用工作流缓存',
                component: 'Switch',
              },
              {
                field: 'ttl',
                label: '缓存过期时间',
                bottomHelpMessage: '缓存过期时间（秒）',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  placeholder: '默认300秒',
                },
              },
              {
                field: 'maxSize',
                label: '最大缓存条数',
                bottomHelpMessage: '最大缓存条数',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  placeholder: '默认100条',
                },
              },
            ],
          },
        },
      ],
    },
    {
      key: 'system.server',
      title: '服务器配置',
      desc: '对服务器进行相关配置',
      schemas: [
        {
          field: 'server',
          label: '基础配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'name',
                label: '服务器名称',
                bottomHelpMessage: '服务器名称（将显示在日志中）',
                component: 'Input',
                componentProps: {
                  placeholder: '默认：XRK Server',
                },
              },
              {
                field: 'host',
                label: '监听地址',
                bottomHelpMessage: '0.0.0.0: 监听所有网络接口，127.0.0.1: 仅监听本地',
                component: 'Input',
                componentProps: {
                  placeholder: '默认：0.0.0.0',
                },
              },
              {
                field: 'url',
                label: '外部访问URL',
                bottomHelpMessage: '用于生成完整的访问链接，留空则自动检测',
                component: 'Input',
                componentProps: {
                  placeholder: 'http://127.0.0.1',
                },
              },
            ],
          },
        },
        {
          field: 'proxy',
          label: '反向代理配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'enabled',
                label: '启用反向代理',
                bottomHelpMessage: '启用后可以实现多域名解析、路径重写、负载均衡等',
                component: 'Switch',
              },
              {
                field: 'httpPort',
                label: 'HTTP端口',
                bottomHelpMessage: 'HTTP监听端口（作为反向代理时建议使用80）',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  max: 65535,
                  placeholder: '默认80',
                },
              },
              {
                field: 'httpsPort',
                label: 'HTTPS端口',
                bottomHelpMessage: 'HTTPS监听端口（作为反向代理时建议使用443）',
                component: 'InputNumber',
                componentProps: {
                  min: 1,
                  max: 65535,
                  placeholder: '默认443',
                },
              },
              {
                field: 'domains',
                label: '域名配置列表',
                bottomHelpMessage: '每个域名可以有不同的配置和处理方式，支持多域名配置',
                component: 'GSubForm',
                componentProps: {
                  multiple: true,
                  schemas: [
                    {
                      field: 'domain',
                      label: '域名',
                      bottomHelpMessage: '域名地址，例如：xrkk.cc 或 *.test.xrkk.cc（支持通配符）',
                      component: 'Input',
                      required: true,
                      componentProps: {
                        placeholder: '请输入域名',
                      },
                    },
                    {
                      field: 'staticRoot',
                      label: '静态文件根目录',
                      bottomHelpMessage: '静态文件根目录路径（可选），留空使用默认www目录',
                      component: 'Input',
                      componentProps: {
                        placeholder: './www',
                      },
                    },
                    {
                      field: 'target',
                      label: '目标服务器',
                      bottomHelpMessage: '反向代理到的目标服务器地址（可选），不配置则直接提供静态文件服务',
                      component: 'Input',
                      componentProps: {
                        placeholder: 'http://localhost:3000',
                      },
                    },
                    {
                      field: 'ssl',
                      label: 'SSL配置',
                      component: 'GSubForm',
                      componentProps: {
                        schemas: [
                          {
                            field: 'enabled',
                            label: '启用SSL',
                            bottomHelpMessage: '是否为此域名启用SSL证书',
                            component: 'Switch',
                          },
                          {
                            field: 'certificate',
                            label: '证书配置',
                            component: 'GSubForm',
                            componentProps: {
                              schemas: [
                                {
                                  field: 'key',
                                  label: '私钥文件路径',
                                  bottomHelpMessage: '域名专用证书私钥路径',
                                  component: 'Input',
                                  componentProps: {
                                    placeholder: '/path/to/domain.key',
                                  },
                                },
                                {
                                  field: 'cert',
                                  label: '证书文件路径',
                                  bottomHelpMessage: '域名专用证书路径',
                                  component: 'Input',
                                  componentProps: {
                                    placeholder: '/path/to/domain.cert',
                                  },
                                },
                                {
                                  field: 'ca',
                                  label: 'CA证书链',
                                  bottomHelpMessage: 'CA证书链路径（可选）',
                                  component: 'Input',
                                  componentProps: {
                                    placeholder: '留空或填写CA证书链路径',
                                  },
                                },
                              ],
                            },
                          },
                        ],
                      },
                    },
                    {
                      field: 'rewritePath',
                      label: '路径重写规则',
                      component: 'GSubForm',
                      componentProps: {
                        schemas: [
                          {
                            field: 'from',
                            label: '源路径',
                            bottomHelpMessage: '需要重写的原始路径，例如：/xrk',
                            component: 'Input',
                            componentProps: {
                              placeholder: '/xrk',
                            },
                          },
                          {
                            field: 'to',
                            label: '目标路径',
                            bottomHelpMessage: '重写后的目标路径，例如：/ 或 /${subdomain}/',
                            component: 'Input',
                            componentProps: {
                              placeholder: '/',
                            },
                          },
                        ],
                      },
                    },
                    {
                      field: 'preserveHostHeader',
                      label: '保持原始Host头',
                      bottomHelpMessage: '某些后端服务需要保持原始Host头',
                      component: 'Switch',
                    },
                    {
                      field: 'ws',
                      label: 'WebSocket支持',
                      bottomHelpMessage: '是否启用WebSocket代理',
                      component: 'Switch',
                    },
                    {
                      field: 'timeout',
                      label: '超时时间',
                      bottomHelpMessage: '代理超时时间（毫秒）',
                      component: 'InputNumber',
                      componentProps: {
                        min: 1000,
                        placeholder: '默认30000毫秒',
                      },
                    },
                    {
                      field: 'headers',
                      label: '自定义请求和响应头',
                      component: 'GSubForm',
                      componentProps: {
                        schemas: [
                          {
                            field: 'request',
                            label: '请求头',
                            bottomHelpMessage: '添加到代理请求的头部，格式：键值对',
                            component: 'Input',
                            componentProps: {
                              placeholder: '例如：{"X-Custom-Header": "value"}',
                            },
                          },
                          {
                            field: 'response',
                            label: '响应头',
                            bottomHelpMessage: '添加到响应的头部，格式：键值对',
                            component: 'Input',
                            componentProps: {
                              placeholder: '例如：{"X-Frame-Options": "SAMEORIGIN"}',
                            },
                          },
                        ],
                      },
                    },
                    {
                      field: 'healthCheck',
                      label: '健康检查配置',
                      component: 'GSubForm',
                      componentProps: {
                        schemas: [
                          {
                            field: 'enabled',
                            label: '启用健康检查',
                            bottomHelpMessage: '是否启用后端健康检查',
                            component: 'Switch',
                          },
                          {
                            field: 'path',
                            label: '检查路径',
                            bottomHelpMessage: '健康检查的路径',
                            component: 'Input',
                            componentProps: {
                              placeholder: '/health',
                            },
                          },
                          {
                            field: 'interval',
                            label: '检查间隔',
                            bottomHelpMessage: '健康检查间隔（毫秒）',
                            component: 'InputNumber',
                            componentProps: {
                              min: 1000,
                              placeholder: '默认30000毫秒',
                            },
                          },
                          {
                            field: 'timeout',
                            label: '检查超时',
                            bottomHelpMessage: '健康检查超时时间（毫秒）',
                            component: 'InputNumber',
                            componentProps: {
                              min: 1000,
                              placeholder: '默认5000毫秒',
                            },
                          },
                        ],
                      },
                    },
                  ],
                },
              },
            ],
          },
        },
        {
          field: 'https',
          label: 'HTTPS配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'enabled',
                label: '启用HTTPS',
                bottomHelpMessage: '是否启用HTTPS服务（全局默认证书）',
                component: 'Switch',
              },
              {
                field: 'certificate',
                label: '默认证书配置',
                bottomHelpMessage: '当域名没有配置专用证书时使用的默认证书',
                component: 'GSubForm',
                componentProps: {
                  schemas: [
                    {
                      field: 'key',
                      label: '私钥文件路径',
                      bottomHelpMessage: '默认私钥文件路径（必需）',
                      component: 'Input',
                      componentProps: {
                        placeholder: '/path/to/default/privkey.pem',
                      },
                    },
                    {
                      field: 'cert',
                      label: '证书文件路径',
                      bottomHelpMessage: '默认证书文件路径（必需）',
                      component: 'Input',
                      componentProps: {
                        placeholder: '/path/to/default/fullchain.pem',
                      },
                    },
                    {
                      field: 'ca',
                      label: 'CA证书链路径',
                      bottomHelpMessage: 'CA证书链文件路径（可选）',
                      component: 'Input',
                      componentProps: {
                        placeholder: '留空表示不使用',
                      },
                    },
                  ],
                },
              },
              {
                field: 'tls',
                label: 'TLS配置',
                component: 'GSubForm',
                componentProps: {
                  schemas: [
                    {
                      field: 'minVersion',
                      label: '最低TLS版本',
                      bottomHelpMessage: '建议不低于TLSv1.2',
                      component: 'Select',
                      componentProps: {
                        options: [
                          { label: 'TLSv1.0', value: 'TLSv1.0' },
                          { label: 'TLSv1.1', value: 'TLSv1.1' },
                          { label: 'TLSv1.2', value: 'TLSv1.2' },
                          { label: 'TLSv1.3', value: 'TLSv1.3' },
                        ],
                        placeholder: '默认TLSv1.2',
                      },
                    },
                    {
                      field: 'http2',
                      label: '启用HTTP/2',
                      bottomHelpMessage: '是否启用HTTP/2协议（提升性能）',
                      component: 'Switch',
                    },
                  ],
                },
              },
              {
                field: 'hsts',
                label: 'HSTS配置',
                component: 'GSubForm',
                componentProps: {
                  schemas: [
                    {
                      field: 'enabled',
                      label: '启用HSTS',
                      bottomHelpMessage: '启用后所有HTTP请求将强制重定向到HTTPS',
                      component: 'Switch',
                    },
                    {
                      field: 'maxAge',
                      label: '有效期',
                      bottomHelpMessage: '有效期（秒），31536000 = 1年',
                      component: 'InputNumber',
                      componentProps: {
                        min: 0,
                        placeholder: '默认31536000',
                      },
                    },
                    {
                      field: 'includeSubDomains',
                      label: '包含子域名',
                      bottomHelpMessage: '是否包含子域名',
                      component: 'Switch',
                    },
                    {
                      field: 'preload',
                      label: '允许预加载',
                      bottomHelpMessage: '是否允许预加载（需要提交到浏览器预加载列表）',
                      component: 'Switch',
                    },
                  ],
                },
              },
            ],
          },
        },
        {
          field: 'static',
          label: '静态文件服务',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'index',
                label: '默认首页文件',
                bottomHelpMessage: '按顺序查找，返回第一个存在的文件',
                component: 'GTags',
                componentProps: {
                  allowAdd: true,
                  allowDel: true,
                  placeholder: '例如：index.html',
                },
              },
              {
                field: 'extensions',
                label: '自动添加扩展名',
                bottomHelpMessage: '是否允许自动添加扩展名，例如：/page 会尝试 /page.html',
                component: 'Switch',
              },
              {
                field: 'cacheTime',
                label: '缓存时间',
                bottomHelpMessage: '缓存总时间，支持格式：1d = 1天, 1h = 1小时',
                component: 'Input',
                componentProps: {
                  placeholder: '默认1d',
                },
              },
            ],
          },
        },
        {
          field: 'security',
          label: '安全配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'helmet',
                label: 'Helmet安全头',
                component: 'GSubForm',
                componentProps: {
                  schemas: [
                    {
                      field: 'enabled',
                      label: '启用Helmet',
                      bottomHelpMessage: '自动添加安全相关的HTTP头部',
                      component: 'Switch',
                    },
                  ],
                },
              },
              {
                field: 'hiddenFiles',
                label: '隐藏文件模式',
                bottomHelpMessage: '匹配这些模式的文件将返回404',
                component: 'GTags',
                componentProps: {
                  allowAdd: true,
                  allowDel: true,
                  placeholder: '例如：^\\..*',
                },
              },
            ],
          },
        },
        {
          field: 'cors',
          label: 'CORS配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'enabled',
                label: '启用CORS',
                bottomHelpMessage: '是否启用跨域资源共享',
                component: 'Switch',
              },
              {
                field: 'origins',
                label: '允许的来源',
                bottomHelpMessage: '* 表示允许所有来源（不安全），建议指定具体域名',
                component: 'GTags',
                componentProps: {
                  allowAdd: true,
                  allowDel: true,
                  placeholder: '例如：https://example.com',
                },
              },
              {
                field: 'methods',
                label: '允许的方法',
                bottomHelpMessage: '允许的HTTP方法',
                component: 'Select',
                componentProps: {
                  mode: 'multiple',
                  options: [
                    { label: 'GET', value: 'GET' },
                    { label: 'POST', value: 'POST' },
                    { label: 'PUT', value: 'PUT' },
                    { label: 'DELETE', value: 'DELETE' },
                    { label: 'OPTIONS', value: 'OPTIONS' },
                    { label: 'PATCH', value: 'PATCH' },
                  ],
                },
              },
              {
                field: 'headers',
                label: '允许的请求头',
                bottomHelpMessage: '允许的请求头',
                component: 'GTags',
                componentProps: {
                  allowAdd: true,
                  allowDel: true,
                  placeholder: '例如：Content-Type',
                },
              },
              {
                field: 'credentials',
                label: '允许凭证',
                bottomHelpMessage: '是否允许携带凭证（cookies等）',
                component: 'Switch',
              },
              {
                field: 'maxAge',
                label: '预检缓存时间',
                bottomHelpMessage: '预检请求缓存时间（秒）',
                component: 'InputNumber',
                componentProps: {
                  min: 0,
                  placeholder: '默认86400秒',
                },
              },
            ],
          },
        },
        {
          field: 'auth',
          label: '认证配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'apiKey',
                label: 'API密钥配置',
                component: 'GSubForm',
                componentProps: {
                  schemas: [
                    {
                      field: 'enabled',
                      label: '启用API密钥',
                      bottomHelpMessage: '是否启用API密钥认证',
                      component: 'Switch',
                    },
                    {
                      field: 'file',
                      label: '密钥存储文件',
                      bottomHelpMessage: '密钥存储文件路径',
                      component: 'Input',
                      componentProps: {
                        placeholder: 'config/server_config/api_key.json',
                      },
                    },
                    {
                      field: 'length',
                      label: '密钥长度',
                      bottomHelpMessage: '密钥长度（字符数）',
                      component: 'InputNumber',
                      componentProps: {
                        min: 16,
                        placeholder: '默认64',
                      },
                    },
                  ],
                },
              },
              {
                field: 'whitelist',
                label: '白名单路径',
                bottomHelpMessage: '无需认证的路径（白名单）',
                component: 'GTags',
                componentProps: {
                  allowAdd: true,
                  allowDel: true,
                  placeholder: '例如：/health',
                },
              },
            ],
          },
        },
        {
          field: 'rateLimit',
          label: '速率限制',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'enabled',
                label: '启用速率限制',
                bottomHelpMessage: '是否启用速率限制（防止恶意请求）',
                component: 'Switch',
              },
              {
                field: 'global',
                label: '全局限制',
                component: 'GSubForm',
                componentProps: {
                  schemas: [
                    {
                      field: 'windowMs',
                      label: '时间窗口',
                      bottomHelpMessage: '时间窗口（毫秒）',
                      component: 'InputNumber',
                      componentProps: {
                        min: 1000,
                        placeholder: '默认900000（15分钟）',
                      },
                    },
                    {
                      field: 'max',
                      label: '最大请求数',
                      bottomHelpMessage: '时间窗口内的最大请求数',
                      component: 'InputNumber',
                      componentProps: {
                        min: 1,
                        placeholder: '默认1000',
                      },
                    },
                    {
                      field: 'message',
                      label: '提示信息',
                      bottomHelpMessage: '超出限制时的提示信息',
                      component: 'Input',
                      componentProps: {
                        placeholder: '请求过于频繁，请稍后再试',
                      },
                    },
                  ],
                },
              },
              {
                field: 'api',
                label: 'API限制',
                component: 'GSubForm',
                componentProps: {
                  schemas: [
                    {
                      field: 'windowMs',
                      label: '时间窗口',
                      bottomHelpMessage: '时间窗口（毫秒）',
                      component: 'InputNumber',
                      componentProps: {
                        min: 1000,
                        placeholder: '默认60000（1分钟）',
                      },
                    },
                    {
                      field: 'max',
                      label: '最大请求数',
                      bottomHelpMessage: '时间窗口内的最大请求数',
                      component: 'InputNumber',
                      componentProps: {
                        min: 1,
                        placeholder: '默认60',
                      },
                    },
                    {
                      field: 'message',
                      label: '提示信息',
                      bottomHelpMessage: '超出限制时的提示信息',
                      component: 'Input',
                      componentProps: {
                        placeholder: 'API请求过于频繁',
                      },
                    },
                  ],
                },
              },
            ],
          },
        },
        {
          field: 'limits',
          label: '请求限制',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'urlencoded',
                label: 'URL编码数据',
                bottomHelpMessage: 'URL编码数据大小限制',
                component: 'Input',
                componentProps: {
                  placeholder: '默认10mb',
                },
              },
              {
                field: 'json',
                label: 'JSON数据',
                bottomHelpMessage: 'JSON数据大小限制',
                component: 'Input',
                componentProps: {
                  placeholder: '默认10mb',
                },
              },
              {
                field: 'raw',
                label: '原始数据',
                bottomHelpMessage: '原始数据大小限制',
                component: 'Input',
                componentProps: {
                  placeholder: '默认50mb',
                },
              },
              {
                field: 'text',
                label: '文本数据',
                bottomHelpMessage: '文本数据大小限制',
                component: 'Input',
                componentProps: {
                  placeholder: '默认10mb',
                },
              },
              {
                field: 'fileSize',
                label: '文件上传',
                bottomHelpMessage: '文件上传大小限制',
                component: 'Input',
                componentProps: {
                  placeholder: '默认100mb',
                },
              },
            ],
          },
        },
        {
          field: 'compression',
          label: '压缩配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'enabled',
                label: '启用压缩',
                bottomHelpMessage: '是否启用响应压缩（减少带宽使用）',
                component: 'Switch',
              },
              {
                field: 'level',
                label: '压缩级别',
                bottomHelpMessage: '0: 无压缩（最快），9: 最大压缩（最慢），推荐：6',
                component: 'InputNumber',
                componentProps: {
                  min: 0,
                  max: 9,
                  placeholder: '默认6',
                },
              },
              {
                field: 'threshold',
                label: '最小压缩大小',
                bottomHelpMessage: '小于此大小的响应不会被压缩（字节）',
                component: 'InputNumber',
                componentProps: {
                  min: 0,
                  placeholder: '默认1024',
                },
              },
            ],
          },
        },
        {
          field: 'logging',
          label: '日志配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'requests',
                label: '记录请求',
                bottomHelpMessage: '是否记录请求日志',
                component: 'Switch',
              },
              {
                field: 'errors',
                label: '记录错误',
                bottomHelpMessage: '是否记录错误日志',
                component: 'Switch',
              },
              {
                field: 'debug',
                label: '调试日志',
                bottomHelpMessage: '是否启用调试日志（详细）',
                component: 'Switch',
              },
              {
                field: 'quiet',
                label: '静默路径',
                bottomHelpMessage: '这些路径的请求不会记录日志',
                component: 'GTags',
                componentProps: {
                  allowAdd: true,
                  allowDel: true,
                  placeholder: '例如：/health',
                },
              },
            ],
          },
        },
        {
          field: 'misc',
          label: '其他配置',
          component: 'GSubForm',
          componentProps: {
            schemas: [
              {
                field: 'detectPublicIP',
                label: '检测公网IP',
                bottomHelpMessage: '是否自动检测公网IP，用于显示外网访问地址',
                component: 'Switch',
              },
              {
                field: 'defaultRoute',
                label: '404重定向',
                bottomHelpMessage: '404时的默认重定向路径',
                component: 'Input',
                componentProps: {
                  placeholder: '默认 /',
                },
              },
            ],
          },
        },
      ],
    },
    {
      key: 'system.redis',
      title: 'Redis配置',
      desc: '对Redis服务器进行相关配置',
      schemas: [
        {
          field: 'host',
          label: 'Redis地址',
          required: true,
          component: 'Input',
          componentProps: {
            placeholder: '请输入Redis地址',
          },
        },
        {
          field: 'port',
          label: 'Redis端口',
          required: true,
          component: 'InputNumber',
          componentProps: {
            placeholder: '请输入Redis端口',
            min: 1,
            max: 65535,
          },
        },
        {
          field: 'username',
          label: 'Redis用户名',
          bottomHelpMessage: '没有用户名可以为空',
          component: 'Input',
          componentProps: {
            placeholder: '请输入Redis用户名',
          },
        },
        {
          field: 'password',
          label: 'Redis密码',
          bottomHelpMessage: '没有密码可以为空',
          component: 'InputPassword',
          componentProps: {
            placeholder: '请输入Redis密码',
          },
        },
        {
          field: 'db',
          label: 'Redis数据库',
          required: true,
          bottomHelpMessage: '一般不用改',
          component: 'InputNumber',
          componentProps: {
            placeholder: '请输入Redis数据库',
          },
        },
      ],
    },
  ],
}

// ========================================
// 群组配置
// ========================================
const groupConfig = () => {
  const funOptions = []
  for (let item of loader.priority) {
    if (item.hasOwnProperty('name') && item.name) {
      if (!funOptions.find(i => i.value === item.name)) {
        funOptions.push({ value: item.name })
      }
    }
  }
  const funComponent = funOptions.length === 0 ? 'GTags' : 'Select'
  return {
    key: 'group',
    title: '群组配置',
    cards: [
      {
        key: 'system.group',
        type: 'keyFormCard',
        title: `{{ form.key === 'default' ? '默认配置' : '群：' + (form?.values?.__GROUP_TIP_TEXT__ ?? form.key) }}`,
        desc: '默认配置对所有群聊生效',
        allowAdd: true,
        allowDel: true,
        addBtnText: '新增群配置',
        promptProps: addGroupPromptProps,
        schemas: [
          {
            field: 'groupGlobalCD',
            label: '整体冷却时间',
            component: 'InputNumber',
            bottomHelpMessage: '群聊中所有指令操作冷却时间，单位毫秒，0则无限制',
            componentProps: {
              placeholder: '请输入指令冷却时间',
            },
          },
          {
            field: 'singleCD',
            label: '个人冷却时间',
            component: 'InputNumber',
            bottomHelpMessage: '群聊中个人操作冷却时间，单位毫秒',
            componentProps: {
              placeholder: '请输入指令冷却时间',
            },
          },
          {
            field: 'onlyReplyAt',
            label: '只关注At',
            component: 'RadioGroup',
            bottomHelpMessage: '是否只仅关注主动@机器人的消息',
            componentProps: {
              options: [
                { label: '关闭', value: 0 },
                { label: '开启', value: 1 },
                { label: '开启、主人关闭', value: 2 },
              ],
            },
          },
          {
            field: 'botAlias',
            label: '机器人别名',
            component: 'GTags',
            bottomHelpMessage: '开启"只关注At"后，发送以别名开头的消息也会响应，支持多个别名',
            componentProps: {
              allowAdd: true,
              allowDel: true,
            },
          },
          ...(CfgAdapter['groupConfig'].group ?? []),
          {
            field: 'addPrivate',
            label: '私聊添加',
            component: 'Switch',
            bottomHelpMessage: '是否允许私聊添加',
            componentProps: {
              checkedValue: 1,
              unCheckedValue: 0,
            },
          },
          {
            field: 'enable',
            label: '功能白名单',
            component: funComponent,
            bottomHelpMessage: '配置后只有配置的功能才可以使用',
            componentProps: {
              allowAdd: true,
              allowDel: true,
              mode: 'multiple',
              options: funOptions,
            },
          },
          {
            field: 'disable',
            label: '功能黑名单',
            component: funComponent,
            bottomHelpMessage: '配置后配置的功能将不可以使用',
            componentProps: {
              allowAdd: true,
              allowDel: true,
              mode: 'multiple',
              options: funOptions,
            },
          },
        ],
      },
    ],
  }
}

// ========================================
// 原神配置
// ========================================
const genshinConfig = {
  key: 'genshin',
  title: '原神配置',
  cards: [
    {
      key: 'genshin.mys.set',
      title: '米游社设置',
      desc: '',
      schemas: [
        {
          field: 'allowUseCookie',
          label: '使用用户ck',
          component: 'Switch',
          bottomHelpMessage: '公共查询是否使用用户ck',
          componentProps: {
            checkedValue: 1,
            unCheckedValue: 0,
          },
        },
        {
          field: 'cookieDoc',
          label: 'ck文档地址',
          component: 'Input',
          bottomHelpMessage: '默认cookie帮助文档链接地址',
          componentProps: {},
        },
        {
          field: 'isAutoSign',
          label: '开启自动签到',
          component: 'Switch',
          bottomHelpMessage: '是否开启米游社原神自动签到',
          componentProps: {
            checkedValue: 1,
            unCheckedValue: 0,
          },
        },
        {
          field: 'signTime',
          label: '签到定时任务',
          component: 'EasyCron',
          bottomHelpMessage: '米游社原神签到定时任务，Cron表达式，默认00:02开始执行，每10s签到一个',
          componentProps: {
            placeholder: '请输入或选择Cron表达式',
          },
        },
        {
          field: 'abbrSetAuth',
          label: '别名权限',
          component: 'RadioGroup',
          bottomHelpMessage: '别名设置权限',
          componentProps: {
            options: [
              { label: '所有群员都可以添加', value: 0 },
              { label: '群主和管理员才能添加', value: 1 },
              { label: '只有主人才能添加', value: 2 },
            ],
          },
        },
      ],
    },
    {
      key: 'genshin.mys.pubCk',
      title: '公共Cookie',
      desc: '米游社公共查询cookie，允许添加多个',
      type: 'arrayFormCard',
      allowAdd: true,
      allowDel: true,
      addBtnText: '添加Cookie',
      lengthMin: 1,
      schemas: [],
    },
    {
      key: 'genshin.gacha',
      title: `十连配置（{{form.key === 'default' ? '默认' : form.key}}）`,
      desc: '十连次数、概率等相关配置',
      type: 'keyFormCard',
      allowAdd: true,
      allowDel: true,
      addBtnText: '新增群单独配置',
      promptProps: addGroupPromptProps,
      schemas: [
        {
          field: 'count',
          label: '每日抽卡数',
          bottomHelpMessage: '设置每天可以抽多少次',
          component: 'InputNumber',
          componentProps: {
            min: 1,
            placeholder: '请输入每日抽卡数',
          },
        },
        {
          field: 'delMsg',
          label: '自动撤回',
          bottomHelpMessage: '自动撤回未出货的抽卡消息，0-120 秒，0 = 不撤回',
          component: 'InputNumber',
          componentProps: {
            placeholder: '请输入自动撤回时间',
          },
        },
        {
          field: 'LimitSeparate',
          label: '分开计算',
          bottomHelpMessage: '角色池、武器池限制次数是否分开计算',
          component: 'Switch',
          componentProps: {
            checkedValue: 1,
            unCheckedValue: 0,
          },
        },
      ],
    },
  ],
}

// ========================================
// 其他配置
// ========================================
const otherConfig = {
  key: 'other',
  title: '其他',
  cards: [
    {
      key: 'system.other',
      title: '其他配置',
      desc: '其他配置',
      schemas: [
        {
          field: 'masterQQ',
          label: '主人QQ',
          bottomHelpMessage: '主人QQ号，功能不受限制，可以设置多个',
          component: 'GSelectFriend',
          componentProps: {
            placeholder: '请选择主人QQ号',
          },
        },
        {
          field: 'disableGuildMsg',
          label: '禁用频道消息',
          bottomHelpMessage: '是否禁用频道消息',
          component: 'Switch',
        },
        {
          field: 'blackQQ',
          label: '黑名单QQ',
          bottomHelpMessage: '黑名单QQ，可以设置多个',
          component: 'GTags',
          componentProps: {
            placeholder: '请输入黑名单QQ',
            allowAdd: true,
            allowDel: true,
          },
        },
        {
          field: 'whiteQQ',
          label: '白名单QQ',
          bottomHelpMessage: '白名单QQ，可以设置多个',
          component: 'GTags',
          componentProps: {
            placeholder: '请输入白名单QQ',
            allowAdd: true,
            allowDel: true,
          },
        },
        ...(CfgAdapter['otherConfig'].other ?? []),
        {
          field: 'blackGroup',
          label: '黑名单群',
          bottomHelpMessage: '黑名单群，可以设置多个',
          component: 'GSelectGroup',
          componentProps: {
            placeholder: '请选择黑名单群',
          },
        },
        {
          field: 'whiteGroup',
          label: '白名单群',
          bottomHelpMessage: '白名单群，可以设置多个',
          component: 'GSelectGroup',
          componentProps: {
            placeholder: '请选择白名单群',
          },
        },
        {
          field: 'autoFriend',
          label: '添加好友',
          bottomHelpMessage: '是否自动同意添加好友请求',
          component: 'Switch',
          componentProps: {
            checkedValue: 1,
            unCheckedValue: 0,
          },
        },
        {
          field: 'autoQuit',
          label: '退群人数',
          bottomHelpMessage: '被好友拉进群时，群人数小于配置值自动退出，设为0表示不处理',
          component: 'InputNumber',
          componentProps: {
            placeholder: '请输入退群人数',
            min: 0,
          },
        },
        {
          field: 'disablePrivate',
          label: '禁用私聊',
          bottomHelpMessage: '禁用后私聊只接受ck以及抽卡链接（Bot主人不受限制）',
          component: 'Switch',
        },
        {
          field: 'disableMsg',
          label: '禁私聊提示',
          bottomHelpMessage: '禁用私聊时Bot的提示内容',
          component: 'Input',
          componentProps: {
            placeholder: '请输入禁用提示',
          },
        },
        {
          field: 'disableAdopt',
          label: '私聊通行字符串',
          bottomHelpMessage: '禁用私聊后，允许响应的字符串',
          component: 'GTags',
          componentProps: {
            allowAdd: true,
            allowDel: true,
          },
        },
      ],
    },
  ],
}

// ========================================
// 导出配置选项卡
// ========================================
export function getConfigTabs() {
  let tabs = []
  tabs.push(baseConfig)
  tabs.push(groupConfig())
  if (hasGenshin) {
    tabs.push(genshinConfig)
  }
  tabs.push(otherConfig)
  return tabs
}

// ========================================
// 配置文件映射
// ========================================
export const configFile = {
  'system.bot': '${botbot}',
  'system.group': '${botgroup}',
  'system.redis': '${botredis}',
  'system.other': '${botother}',
  'system.server': '${botserver}',
  'system.db': '${botdb}',
  'system.device': '${botdevice}',
  'system.notice': '${botnotice}',
  'system.renderer': '${botrenderer}',
  'system.aistream': '${botaistream}',
  'genshin.gacha': '/plugins/genshin/config/gacha.set.yaml',
  'genshin.mys.pubCk': '/plugins/genshin/config/mys.pubCk.yaml',
  'genshin.mys.set': '/plugins/genshin/config/mys.set.yaml',
  'genshin.role.name': '/plugins/genshin/config/role.name.yaml',
}